#!/bin/bash

# 需要使用ssh免密操作
# 使用ssh-keygen命令生成公钥，并将公钥写入YOCTO服务器的.ssh/authorized_keys文件中

#ftp info
WORK_PATH=$(readlink -f ${0} | xargs dirname)
FTP_IP="10.1.19.164"
FTP_PORT="21"
FTP_ARENE_NAME="tagldev"
FTP_NAME="dten_repo"
FTP_PASS="arene_IVI-=00"
FTP_ARENE_URL="ftp://${FTP_ARENE_NAME}:arene%5FIVI-%3D00@${FTP_IP}:${FTP_PORT}/sdk_docker"
FTP_BEV_RELEASE_URL="ftp://${FTP_NAME}:arene%5FIVI-%3D00@${FTP_IP}:${FTP_PORT}/release"
FTP_BEV_URL="ftp://${FTP_NAME}:arene%5FIVI-%3D00@${FTP_IP}:${FTP_PORT}"
BEV_DEBUG_RUNTIME_DOCKER="arene-image-cockpit-cdc-genericx86-64.tar.bz2"

#variable for sdk
ENV_PATH="${WORK_PATH}/env"
DOCKER_CONFIGS_PATH="${ENV_PATH}/dn-cdc-lvgvm-misc-simulator"
DOCKER_CONFIGS_FILE="${DOCKER_CONFIGS_PATH}/config/docker.env"
DOCKER_SCRIPT="${DOCKER_CONFIGS_PATH}/dn-sim.sh"
DOCKER_CONTAINER_NAME="dn-sim"
DN_CONTAINER_NAME="dn-sim"
DOCKER_CONFIGS_URL="https://gitlab.geniie.net/BEVS3CDC/dn-bevs3-cdc/dn-cdc-lvgvm-misc-simulator.git"
BEV_RELEASE_URL="https://art.geniie.net/artifactory/bevs3cdc-generic-tier1/devops/dn-cdc-lvgvm-26bev-repo/release"
BEV_RELEASE_URL_DEBUG_FILE="x86-ubuntu/rootfs/debug/genericx86-64/arene-image-cockpit-cdc-genericx86-64.tar.bz2"
BEV_RELEASE_URL_RELEASE_FILE="x86-ubuntu/rootfs/release/genericx86-64-release/arene-image-cockpit-cdc-genericx86-64.tar.bz2"
DOCKEER_PULL_USER="xiufd@neusoft.com"
DOCKEER_PULL_PASSWD='!QAZ1qaz'

LOG_PATH="${WORK_PATH}/log"
DATE=$(date +%Y%m%d-%H%M%S)
start_time_s=$(date +%s)
SHOW_UNIT_STATE="/opt/arene/test/arene-ivi-rootfs/system/run-target.sh"
USER_ID=$(id -u)
USER_PASSWORD='tagldev11!!'
# USER_PASSWORD='bevdev11!!'
IMAGES_FILE="${ENV_PATH}/IMAGES_FILE"

BT_CONTAINER_NAME="sdk-hal-docker-cdc_arene_1_bt"
########################################################################################################################
declare -A TEST_TARGET=(
    # [unit]="service_file:test_command"
    # [lansrv]="lansrvhal_stub.service:/opt/dc-ivi-pf/tests/dn-cdc-lvgvm-hal-lansrv-impl-qc/run-target.sh"
    # [avclan]="avclan.service:/opt/dc-ivi-pf/tests/dn-cdc-lvgvm-hal-avclan-impl-qc/run-target.sh"
    # [most]="mosthal_stub.service:/opt/dc-ivi-pf/tests/dn-cdc-lvgvm-hal-most-impl-qc/run-target.sh --type it --level 1"
    [usb]="blkmng_usb_hal_svc.service:/opt/dc-ivi-pf/tests/usbhal/run-target.sh --type it --level 1"
    # [backup]="backuphal_stub.service:/opt/dc-ivi-pf/tests/dn-cdc-lvgvm-hal-backup-impl-qc/run-target.sh --type it --level 1"
    # [input]="input.service:/opt/dc-ivi-pf/tests/input_hal/run-target.sh"
    # [cec]="cechal.service:/opt/dc-ivi-pf/tests/dn-cdc-lvgvm-hal-cec-impl-qc/run-target.sh --type it --level 1"


    # [dtv]="dtv.service:/opt/dc-ivi-pf/tests/dn-cdc-lvgvm-hal-dtv-api/run-target.sh --type it --level 1"
    # [radio]="radio.service:/opt/dc-ivi-pf/tests/dn-cdc-lvgvm-hal-radio/run-target.sh --type it --level 1"
    # [vehilce]="vehicle_hal_stub.service:/opt/dc-ivi-pf/tests/vehicle_hal/run-target.sh --type hal"
    # [wifi]="wifi_hal_svc.service:/opt/dc-ivi-pf/tests/dn-cdc-hal-wifi/run-target.sh --type it --level 1 --filter WifiHalApiTest.*"
    # [cinemoex-sdk]="cinemoex_hal_svc.service:/opt/dc-ivi-pf/tests/dn-cdc-hal-cinemoex/cinemo_sdk/run-target.sh --type it --level 1"
    # [cinemoex-hal]="cinemoex_hal_svc.service:/opt/dc-ivi-pf/tests/dn-cdc-hal-cinemoex/cinemoex_hal/run-target.sh --type it --level 1"
    # [bt]="bt_hal_svc_stub.service:/opt/dc-ivi-pf/tests/dn-cdc-hal-bluetooth/run-target.sh --type it --level 1"
    # [sxm]="sxm.service:/opt/dc-ivi-pf/tests/dn-cdc-hal-sxm/run-target.sh --type it --level 1"
    # [power]="power_hal_svc.service:/opt/dc-ivi-pf/tests/dn-cdc-hal-power-impl-qc/run-target.sh --type it --level 1"
)

declare -A TEST_UNIT_SERVICE_NAME=(
    [lansrv]="aglhal_svc:sysdb_fo_bridge"
    [avclan]="aglhal_svc"
    [most]="most_hal"
    [cec]="cec_test_driver:cec_hal_svc"
    [usb]="usb_hal_test_bridge:usb_hal_svc:devmng_cmd:backuphal"
    [backup]="backup_hal:backup_hal_stub_bkup"
    [input]="aglhal_svc:peripheral_svc:DelPersonalSrv:settings_provider"
    [power]="power_hal_svc"
    [cinemoex_hal]="cinemo_stub_test:cinemo_stub_server:cinemo_stub_testdriver:cinemoex_hal_svc"
    [cinemoex_sdk]="cinemo_stub_test:cinemo_stub_server:cinemo_stub_testdriver:cinemoex_hal_svc"
    [sxm]="sxm_hal:backuphal_stub_bkup_env:backuphal_stub_bkup"
    [bt]="gtest_hal_bt:bt_stack_adapter:scenario_hal_bt_server"
    [wifi]="libnl_server:wifi_hal_svc"
    [vehicle]="stub_vehiclehal:aglhal_svc:vehicle"
)

declare -A DOCKER_DOWNLOAD=(
    [bev@srv005]="bev@172.30.10.5:/home/bev/sdk_build_work/env/image"
    [wovendev@PC1798]="wovendev@172.30.10.8:/home/wovendev/sdk_build_work/env/image"
)
########################################################################################################################

function log()
{
    echo "----------------------------------------------------------------------------------------------------------------"
    echo ${1}
    echo "---"
}

function start_run
{
    if [ -e ${ENV_PATH} ]; then
        log "[NORMAL] ----[${ENV_PATH}] existed !!!----"
    else
        mkdir ${ENV_PATH}
        log "[NORMAL] --create ${ENV_PATH} OK!"
    fi

    if [[ -e ${LOG_PATH} ]]; then
        log "[NORMAL] ----[${LOG_PATH}] existed !!!----"
        find ${LOG_PATH} -mtime +10 -exec rm -rf {} \;
    else
        mkdir ${LOG_PATH}
        log "[NORMAL] --create ${ENV_PATH} OK!"
    fi
    echo "#####################################################"
    echo "# Execute Command: $@"
    echo "# Start Time     : $(date +%Y/%m/%d-%H:%M:%S)"
    echo "#####################################################"
}

function normal_exit
{
    # delete lock file.
    local end_time_s=$(date +%s)
    echo "#####################################################"
    echo "# The execute command ($@) normal end."
    echo "# End Time       : $(date +%Y/%m/%d-%H:%M:%S)"
    echo "# Execution Time : $[ $end_time_s - $start_time_s ]s"
    echo "# The Log log storage path => ${LOG_PATH}"
    echo "#####################################################"
    exit 0
}

function docker_restart
{
    echo ">>>>>>>>>>> docker_restart start >>>>>>>>>>>>"
    echo ${USER_PASSWORD} | sudo -S apt -v >/dev/null 2>&1
    (${DOCKER_SCRIPT} restart)
    if [[ $? -ne 0 ]]; then
        echo "[ERR ] Failed to restart docker container."
        exit 1
    fi
    echo "[INFO] Successfully restart docker container."
    sleep 10
    echo "<<<<<<<<<<< docker_restart end <<<<<<<<<<<<<"
    echo ""
}

function env_ready() 
{
    if [ -e ${ENV_PATH} ]; then
        log "[NORMAL] ----[${ENV_PATH}] existed !!!----"
    else
        mkdir ${ENV_PATH}
        log "[NORMAL] --create ${ENV_PATH} OK!"
    fi

    local dn_docker_status=$(docker ps | grep ${DOCKER_CONTAINER_NAME})
    if [[ -n ${dn_docker_status} ]]; then
        (${DOCKER_SCRIPT} stop 2>&1)
        if [[ $?  -ne 0 ]]; then
            log "[ERROR] sh = [${DOCKER_SCRIPT} stop] ERROR !!!"
            exit 1
        fi
    fi
    
    local container_id=$(docker ps -a -q --filter name="${DOCKER_CONTAINER_NAME}")
    if [[ -n ${container_id} ]]; then
        docker rm -f ${container_id}
    fi
    container_id=$(docker ps -a -q --filter name="${BT_CONTAINER_NAME}")
    if [[ -n ${container_id} ]]; then
        docker rm -f ${container_id}
    fi

    # stop host weston
    local weston_status=$(ps -ef | grep [w]eston)
    if [[ -n ${weston_status} ]]; then
        start-stop-daemon -K -p /tmp/weston.pid && {
            log "[INFO] Succeeded to stop weston."
        } || {
            log "[ERROR] Failed to stop weston !!!" 
            log "[INFO]  kill weston process !!!" 
            if pgrep weston >/dev/null; then
                kill -9 $(pgrep weston)
            fi
        }
    fi
    local retry_num=0
    while pgrep weston >/dev/null; do
        sleep 10
        if [[ ${retry_num} == 10 ]]; then
            log "[ERROR] weston stop check ERROR !!!"
            exit 1
        fi
        retry_num=$((retry_num + 1))
    done
}

function git_clone_config_repo() 
{
    log "[NORMAL ] git clone ${DOCKER_CONFIGS_URL} ${DOCKER_CONFIGS_PATH}."
    git clone ${DOCKER_CONFIGS_URL} ${DOCKER_CONFIGS_PATH}
    git -C ${DOCKER_CONFIGS_PATH} pull
    git -C ${DOCKER_CONFIGS_PATH} checkout master

    sed -i "s@sudo -v@echo '${USER_PASSWORD}' | sudo -S -v@" ${DOCKER_CONFIGS_PATH}/setup.sh
    echo "${USER_PASSWORD}" | sudo -S apt -v >/dev/null 2>&1
    ${DOCKER_CONFIGS_PATH}/setup.sh

    sed -ri "s@WORK_DIR=~/hmifw_share@WORK_DIR=${WORK_PATH}@g" ${DOCKER_CONFIGS_FILE}
    sed -i 's|DOCKER_IMAGE_RUNTIME_NAME=.*|DOCKER_IMAGE_RUNTIME_NAME=dn-bevs3cdc-runtime-dbg|g' ${DOCKER_CONFIGS_FILE}

    sed -i '/^function start_container() {$/a\    # set virtual display \
    Xvfb_info=$(ps -ef | grep Xvfb) \
    if [[ ${Xvfb_info} =~ "Xvfb :2 -screen 0 1280x720x24" ]]; then \
        echo "[info] Xvfb is exist info = [${Xvfb_info}]" \
    else \
        echo "[info] sh = [Xvfb :2 -screen 0 1280x720x24 &]" \
        Xvfb :2 -screen 0 1280x720x24 & \
        sleep 30 \
    fi \
    export DISPLAY=:2.0 \
    sleep 5' ${DOCKER_SCRIPT}
    sed -i '/launch_weston \${WESTON_INI}/a\    logger "INFO" "Check the Weston startup status, please wait for about 3 minutes." \
    local retry_num=0 \
    while ! pgrep weston >/dev/null; do \
        logger "INFO" "Check weston startup status [${retry_num}] times." \
        sleep 10 \
        if [[ ${retry_num} == 20 ]]; then \
            logger "ERROR" "weston startup ERROR !!!" \
            exit 1 \
        fi \
        retry_num=$((retry_num + 1)) \
    done \
    logger "INFO" "Weston startup Succeeded."' ${DOCKER_SCRIPT}
    sed -i 's|docker stop ${CONTAINER_NAME} > /dev/null|docker stop ${CONTAINER_NAME} > /dev/null 2>\&1|g' ${DOCKER_SCRIPT}
    sed -i '/logger "ERROR" "Failed to stop container."/a\        if pgrep weston >/dev/null; then \
            kill -9 $(pgrep weston) \
        fi' ${DOCKER_SCRIPT}
    sed -i '/logger "ERROR" "Failed to kill weston."/a\        if pgrep weston >/dev/null; then \
            kill -9 $(pgrep weston) \
        fi' ${DOCKER_SCRIPT}
    sed -i '/Delete temporary files no longer needed/a\    logger "INFO" "Check the Weston stop status, please wait for about 1 minutes." \
    local retry_num=0 \
    while pgrep weston >/dev/null; do \
        logger "INFO" "Check weston stop status [${retry_num}] times." \
        sleep 10 \
        if [[ ${retry_num} == 10 ]]; then \
            logger "ERROR" "weston stop ERROR !!!" \
            exit 1 \
        fi \
        retry_num=$((retry_num + 1)) \
    done \
    logger "INFO" "Weston stop Succeeded."' ${DOCKER_SCRIPT}

    # set sudo password
    if [[ ${PIPESTATUS[1]} -eq 0 ]]; then 
        if echo "${USER_PASSWORD}" | grep -q "[']"; then
            sed -i "/^function start_container() {$/a\    echo \"${USER_PASSWORD}\" | sudo -S -v" ${DOCKER_SCRIPT}
            sed -i "/^function stop_container() {$/a\    echo \"${USER_PASSWORD}\" | sudo -S -v" ${DOCKER_SCRIPT}
            sed -i "/^function kill_container() {$/a\    echo \"${USER_PASSWORD}\" | sudo -S -v" ${DOCKER_SCRIPT}
        else
            sed -i "/^function start_container() {$/a\    echo '${USER_PASSWORD}' | sudo -S -v" ${DOCKER_SCRIPT}
            sed -i "/^function stop_container() {$/a\    echo '${USER_PASSWORD}' | sudo -S -v" ${DOCKER_SCRIPT}
            sed -i "/^function kill_container() {$/a\    echo '${USER_PASSWORD}' | sudo -S -v" ${DOCKER_SCRIPT}
        fi
    fi
    local delete_line_no=$(grep -n 'device /dev/dri:/dev/dri' ${DOCKER_SCRIPT} | head -1 | cut -d ":" -f1)
    sed -i "${delete_line_no}d" ${DOCKER_SCRIPT}
    # sed -ri 's@^\s+--device /dev/dri:/dev/dri @1111111@g' dn-sim.sh
    # sed -ri '/1111111/d' dn-sim.sh
    local system_ver=$(cat /etc/issue | cut -d " " -f2)
    if [[ ${system_ver} =~ "22.04" ]]; then
        sed -i 's/--cgroup-parent/--cgroupns/g' ${DOCKER_SCRIPT}
    fi
}

function common_check()
{
    local Xvfb_info=$(ps -ef | grep [X]vfb)
    if [[ ${Xvfb_info} =~ "Xvfb :2 -screen 0 1280x720x24" ]]; then
        log "[info] Xvfb is exist info = [${Xvfb_info}]"
    else
        local xvfb_path=$(which Xvfb)
        if [[ -z ${xvfb_path} ]]; then
            sudo apt update
            sudo apt install xvfb x11vnc
        else
            log "[info] sh = [Xvfb :2 -screen 0 1280x720x24 &], Please wait 30 seconds !"
            Xvfb :2 -screen 0 1280x720x24 &
            sleep 30
        fi
    fi
}

function Execute_install_script() 
{
    common_check

    if [[ ${1} ]]; then
        source ${DOCKER_CONFIGS_FILE}
        log "[NORMAL ] docker rmi images=${DOCKER_REGISTRY}/${DOCKER_IMAGE_RUNTIME_NAME}:${DOCKER_IMAGE_RUNTIME_TAG}."
        docker rmi ${DOCKER_REGISTRY}/${DOCKER_IMAGE_RUNTIME_NAME}:${DOCKER_IMAGE_RUNTIME_TAG}
        log "[info] sh = [${DOCKER_SCRIPT} load ${1}]"
        (${DOCKER_SCRIPT} load ${1})
        if [[ $?  -ne 0 ]]; then
            echo "[info] sh = [${DOCKER_SCRIPT} load ${1}] ERROR !!!"
            exit 1
        fi
    fi
    log "[info] sh = [${DOCKER_SCRIPT} start]"
    (${DOCKER_SCRIPT} start 2>&1)
    if [[ $?  -ne 0 ]]; then
        echo "[info] sh = [${DOCKER_SCRIPT} start] ERROR !!!"
        exit 1
    fi
}

function install_docker_env()
{
    env_ready

    if [[ ${1} ]];then
        log "[INFO ] version is [${1}] !!!."
        log "[NORMAL ] rm -rf ${DOCKER_CONFIGS_PATH}"
        rm -rf ${DOCKER_CONFIGS_PATH}

        git_clone_config_repo
        sed -ri "s|DOCKER_IMAGE_RUNTIME_TAG=.*|DOCKER_IMAGE_RUNTIME_TAG=${1}|g" ${DOCKER_CONFIGS_FILE}
        # download docker
        log "[NORMAL ] wget ${BEV_RELEASE_URL}/${1:0:6}/${1}/${BEV_RELEASE_URL_DEBUG_FILE}"
        wget ${BEV_RELEASE_URL}/${1:0:6}/${1}/${BEV_RELEASE_URL_DEBUG_FILE} -O ${DOCKER_CONFIGS_PATH}/${BEV_DEBUG_RUNTIME_DOCKER}
        if [[ $?  -ne 0 ]]; then
            log "[ERROR ] wget ${BEV_RELEASE_URL}/${1:0:6}/${1}/${BEV_RELEASE_URL_DEBUG_FILE} -O ${DOCKER_CONFIGS_PATH}/${BEV_DEBUG_RUNTIME_DOCKER} ERROR !!!."
            return 1
        fi
    else
        log "[ERROR ] CHECK IMAGES_VERSION=[${1}] ERROR !!!."
        return 1
    fi

    Execute_install_script ${DOCKER_CONFIGS_PATH}/${BEV_DEBUG_RUNTIME_DOCKER}
    if [ -e ${IMAGES_FILE} ]; then
        # remove old docker images.
        image_info=$(cat ${IMAGES_FILE})
        for image in ${image_info[@]}; do
            log "[NORMAL ] docker rmi images=${image}"
            docker rmi ${image}
        done
    fi
    source ${DOCKER_CONFIGS_FILE}
    echo "${DOCKER_REGISTRY}/${DOCKER_IMAGE_RUNTIME_NAME}:${DOCKER_IMAGE_RUNTIME_TAG}" > ${IMAGES_FILE}
    echo ${USER_PASSWORD} | sudo rm -rf ${LOG_PATH}/*
}

function install_sdk_docker () 
{
    if [ -e ${DOCKER_CONFIGS_PATH} ]; then
        log "[NORMAL] ----[${DOCKER_CONFIGS_PATH}] existed !!!----"
        env_ready
    else
        log "[ERRPR] -- ${DOCKER_CONFIGS_PATH} does not exist !!!"
        exit 1
    fi
    
    pushd ${DOCKER_CONFIGS_PATH}
        local DOWNLOAD_URL=${DOCKER_DOWNLOAD[${1}]}
        rm -rf ${DOWNLOAD_URL##*/}
        log "[NORMAL] scp -r ${DOWNLOAD_URL}} start"
        scp -r ${DOWNLOAD_URL} ./
        if [[ -e ${DOWNLOAD_URL##*/} ]];then
            pushd ${DOWNLOAD_URL##*/}
                local runtime_file=$(ls *.tar.gz)
                if [[ -e ${runtime_file} ]];then
                    log "[NORMAL] docker load -i ${runtime_file}"
                    docker load -i ${runtime_file}
                else
                    echo -e "\033[31m[ERR ] sdk build runtime_file does not exist !!!"
                    exit 1
                fi
            popd    
        else
            echo -e "\033[31m[ERR ] Failed to scp -r ${DOWNLOAD_URL} !!!\033[0m"
            exit 1
        fi
    popd
    log "[NORMAL] scp ${DOWNLOAD_URL} end "

    runtime_tag=${runtime_file/.tar.gz/}
    sed -ri "s|DOCKER_IMAGE_RUNTIME_TAG=.*|DOCKER_IMAGE_RUNTIME_TAG=${runtime_tag}|g" ${DOCKER_CONFIGS_FILE}

    log "[info] sh = [${DOCKER_SCRIPT} start]"
    (${DOCKER_SCRIPT} start 2>&1)
    if [[ $?  -ne 0 ]]; then
        echo "[info] sh = [${DOCKER_SCRIPT} start] ERROR !!!"
        exit 1
    fi
    source ${DOCKER_CONFIGS_FILE}
    echo "${DOCKER_REGISTRY}/${DOCKER_IMAGE_RUNTIME_NAME}:${DOCKER_IMAGE_RUNTIME_TAG}" >> ${IMAGES_FILE}
}

function bev_unit_test()
{
    echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> [${1}_IT_${2}] docker_unit_test start time = [$(date +%Y/%m/%d-%H:%M:%S)][$(date +%s)] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
    for test_unit_name in ${!TEST_TARGET[@]} ; do
        local test_unit_info=${TEST_TARGET[${test_unit_name}]}
        local service_file=$(echo ${test_unit_info} | cut -d : -f 1)
        local test_command=$(echo ${test_unit_info} | cut -d : -f 2)
        local docker_log_path="/home/root/share/log/${3}/${1}_IT_${2}/${test_unit_name}"
        local test_path="${LOG_PATH}/${3}/${1}_IT_${2}/${test_unit_name}"
        local test_log="${test_path}/${test_unit_name}_test_result.log"
        local test_result_file="${test_path}/aggregated_${test_unit_name}_result.txt"
        mkdir -p ${test_path}
        : > ${test_result_file}
        : > ${test_log}
        echo ""
        echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> [${test_unit_name}] test start time = [$(date +%Y/%m/%d-%H:%M:%S)][$(date +%s)] >>>>>>>>>>>>"
        local DOCKER_CONTAINER_NAME=${DN_CONTAINER_NAME}
        if [[ "create" == ${1} ]];then
            if [[ ${test_unit_name} == "backup" ]]; then
                echo "backup does not perform create test !!!" | tee -a ${test_log}
                echo "backup does not perform create test !!!" | tee -a ${test_result_file}
                continue
            fi
            log "[INFO]  docker rm -f ${DOCKER_CONTAINER_NAME} start"
            env_ready
            log "[INFO]  docker rm -f ${DOCKER_CONTAINER_NAME} end "
            log "[info] sh = [${DOCKER_SCRIPT} start]"
            (${DOCKER_SCRIPT} start 2>&1)
            if [[ $?  -ne 0 ]]; then
                log "[ERR] sh = [${DOCKER_SCRIPT} start] ERROR !!!"
                exit 1
            fi
            log "[info] sh = [${DOCKER_SCRIPT} end]"
            sleep 10
        else
            if docker ps | grep ${DOCKER_CONTAINER_NAME} >/dev/null; then
                log "[info] docker [${DOCKER_CONTAINER_NAME}] is exists, Restart to test !!!"
            else
                docker_restart
            fi
            # delete invalid contents
            # # delete dlt log
            docker exec ${DOCKER_CONTAINER_NAME} bash -c "rm -fr /var/log/dlt/*; sed -ri 's@NOFiles=2@NOFiles=100@g' /etc/dlt_logstorage.conf; sync; exit"
            if [[ $? -ne 0 ]]; then
                echo "[ERR ] Failed to delete dlt log for unit ${test_unit_name}."
                # exit 1
            fi                            
            echo "[INFO] Successfully delete dlt log for unit ${test_unit_name}."
            
            if [[ ${test_unit_name} == "backup" ]]; then
                echo "Test execution [/opt/config_json_merge] command !!!"
                docker exec ${DOCKER_CONTAINER_NAME} bash -c "
                chmod +x /home/root/share/test_file/config_json_merge; \
                /home/root/share/test_file/config_json_merge  /opt/dc-ivi-pf/share/service/backup_hal/config.json /usr/local/share/stub/backuphal/test_config.json /usr/local/share/stub/backuphal/merged_config.json; \
                cp -f /opt/dc-ivi-pf/share/service/backup_hal/config.json  /usr/local/share/stub/backuphal/tier1_config_bk.json; \
                cp -f /usr/local/share/stub/backuphal/merged_config.json /opt/dc-ivi-pf/share/service/backup_hal/config.json; \
                dlt-logstorage-ctrl -s /var/log/dlt; \
                rm -rf /var/log/dlt/*; \
                rm /var/backup/fs/* -rf; \
                sync; \
                exit 0"
                # jq '.categories += ( inputs | .categories )' config.json test_config.json > temp.json && mv temp.json config.json || inner_ret=$?
            fi

            echo "[INFO] rm -rf /var/backup/fs/* for unit ${test_unit_name}."
            docker exec ${DOCKER_CONTAINER_NAME} bash -c "rm -rf /var/backup/fs/*; sync; exit"
            #################################################################################
            # restart docker container
            #################################################################################
            docker_restart
        fi

        #################################################################################
        # check weston status
        #################################################################################
        log "[info] weston check"
        local retry_num=0
        while ! pgrep weston >/dev/null; do 
            sleep 10
            if [[ ${retry_num} == 10 ]]; then
                break
            fi
            retry_num=$((retry_num + 1))
        done
        if [[ ${retry_num} == 10 ]]; then
            log "[ERR ] weston startup Error !!!" | tee -a ${test_result_file}
            echo ""  | tee -a ${test_result_file}
            continue
        fi

        if [[ ${test_unit_name} == "vehilce" ]]; then
            echo "docker exec systemctl stop vehicle !"
            docker exec ${DOCKER_CONTAINER_NAME} bash -c "systemctl stop vehicle; exit"
            # echo "docker exec systemctl stop peripheral_svc !"
            # docker exec ${DOCKER_CONTAINER_NAME} bash -c "systemctl stop peripheral_svc; exit"
            sleep 10
        fi

        if [[ ${test_unit_name} == "bt" ]]; then
            local container_id=$(docker ps -a -q --filter name="${BT_CONTAINER_NAME}")
            if [[ "create" == ${1} ]];then
                if [[ -n ${container_id} ]]; then
                    docker rm -f ${container_id}
                fi
                echo "echo 0 | sudo tee /proc/sys/kernel/randomize_va_space !"
                echo 0 | sudo tee /proc/sys/kernel/randomize_va_space
                source ${DOCKER_CONFIGS_FILE}
                echo "docker run -d --privileged -v ${WORK_PATH}:/home/root/share --name ${BT_CONTAINER_NAME} ${DOCKER_REGISTRY}/${DOCKER_IMAGE_RUNTIME_NAME}:${DOCKER_IMAGE_RUNTIME_TAG} /sbin/init"
                docker run -d --privileged -v ${WORK_PATH}:/home/root/share --name ${BT_CONTAINER_NAME} ${DOCKER_REGISTRY}/${DOCKER_IMAGE_RUNTIME_NAME}:${DOCKER_IMAGE_RUNTIME_TAG} /sbin/init
                sleep 1
                local bt_id=$(docker ps -a -q --filter name="${BT_CONTAINER_NAME}")
                if [[ -n ${bt_id} ]]; then
                    DOCKER_CONTAINER_NAME=${BT_CONTAINER_NAME}
                else
                    log "CONTAINER_NAME [${DOCKER_CONTAINER_NAME}] not exist bt test ERROR !!!" | tee -a ${test_result_file}
                    continue
                fi
            else
                if [[ -n ${container_id} ]]; then
                    echo "docker restart ${BT_CONTAINER_NAME}"
                    docker restart ${BT_CONTAINER_NAME}
                    DOCKER_CONTAINER_NAME=${BT_CONTAINER_NAME}
                    sleep 10
                else
                    log "CONTAINER_NAME [${DOCKER_CONTAINER_NAME}] not exist bt test ERROR !!!" | tee -a ${test_result_file}
                    continue
                fi
            fi
            echo "docker exec ${DOCKER_CONTAINER_NAME} bash install_stub_bthl.sh uartBT.ko and systemctl restart bt_hal_svc_stub.service !"
            docker exec ${DOCKER_CONTAINER_NAME} bash -c "/usr/local/share/stub/bt_hal/bin/install_stub_bthl.sh /home/root/share/test_file/uartBT.ko; \
                                                            systemctl restart bt_hal_svc_stub.service; exit 0" | tee -a ${test_log}
            if grep "created /dev/uartBT device node successfully" ${test_log} > /dev/null; then
                log "[INFO] created /dev/uartBT device node successfully !!!" | tee -a ${test_result_file}
            else
                log "[ERR] created /dev/uartBT device node failed !!!" | tee -a ${test_result_file}
            fi
            sleep 1
        fi

        if [[ ${test_unit_name} == "usb" ]]; then
            echo "docker exec mkdir -p /mnt/media/usb1 !"
            docker exec ${DOCKER_CONTAINER_NAME} bash -c "mkdir -p /mnt/media/usb1; sync; exit 0"
            sleep 1
        fi
        
        log "[info] PowerState|Normal check"
        local unit_state_log="${test_path}/unit_state.log"
        docker exec ${DOCKER_CONTAINER_NAME} bash -c "${SHOW_UNIT_STATE}" > ${unit_state_log} 2>&1
        retry_num=0
        while grep -Ev 'PowerState|Normal|-' ${unit_state_log} > /dev/null; do
            sleep 10
            docker exec ${DOCKER_CONTAINER_NAME} bash -c "${SHOW_UNIT_STATE}" > ${unit_state_log} 2>&1
            if [[ ${retry_num} == 10 ]]; then
                break
            fi
            retry_num=$((retry_num + 1))
        done
        if [[ ${retry_num} == 10 ]]; then
            log "[ERR ] PowerState Error !!!" | tee -a ${test_result_file}
            grep -Ev 'PowerState|Normal|-' ${unit_state_log} | tee -a ${test_result_file}
            echo ""  | tee -a ${test_result_file}
            # echo "[INFO] Start copy dlt log for unit ${test_unit_name}."
            # docker exec ${DOCKER_CONTAINER_NAME} bash -c "dlt-logstorage-ctrl -s /var/log/dlt;\
            #                                         gzip -dk /var/log/dlt/journal.*.dlt.gz; \
            #                                         find /var/log/dlt/ -name "journal.*.dlt" | xargs dlt-convert -a >> /var/log/dlt/journal_dlt_convert.log; \
            #                                         cp -rf /var/log/dlt/ ${docker_log_path} ;\
            #                                         rm -f ${docker_log_path}/dlt/dlt_logstorage.conf ;\
            #                                         chown -R ${USER_ID}:${USER_ID} ${docker_log_path}/ ;\
            #                                         cd ${docker_log_path}/ ;\
            #                                         ps -ef > system_process.txt ;\
            #                                         journalctl > service_start.log ;\
            #                                         chown -R ${USER_ID}:${USER_ID} system_process.txt ;\
            #                                         chown -R ${USER_ID}:${USER_ID} service_start.log ;\
            #                                         exit 0"
            # continue
        fi

        if [[ ${test_unit_name} == "power" ]]; then
            echo "docker exec echo '0' > /tmp/stub_pwrh/fobs/fobs_acc !"
            docker exec ${DOCKER_CONTAINER_NAME} bash -c "echo '0' > /tmp/stub_pwrh/fobs/fobs_acc; sync; exit 0"
            sleep 60
            docker exec ${DOCKER_CONTAINER_NAME} bash -c "${SHOW_UNIT_STATE}" > ${test_path}/unit_state_power.log 2>&1
            if grep -Ev 'PowerState|Parking|-' ${test_path}/unit_state_power.log > /dev/null ; then
                echo "[ERR ] grep -Ev 'PowerState|Parking|-' ${test_path}/unit_state_power.log ERROR !!!."
                echo "[ERR ] Unit [${test_unit_name}] test execution result failed !!!." | tee -a ${test_result_file}
            else
                echo "[INFO ] grep -Ev 'PowerState|Parking|-' ${test_path}/unit_state_power.log SUCC." 
                echo "[INFO ] Unit [${test_unit_name}] test execution result succ !!!." | tee -a ${test_result_file}
            fi
            continue
        fi

        log "[info] init_enter.rc system status check]"
        docker exec ${DOCKER_CONTAINER_NAME} bash -c "ps -eo euser,pid,ppid,cmd | grep -v ps" > ${test_path}/test_before_process.log
        docker exec -u default ${DOCKER_CONTAINER_NAME} bash -c "\
        cd /home/default ;\
        :>./x86_dbf_status_before.log ;\
        for line_no in \$(grep -n -E '^service|service base_svc' '/opt/arene/share/service/sys_blk_mng/init_enter.rc' | cut -d ':' -f1); do \
          service_name=\$(sed -n \"\${line_no}p\" /opt/arene/share/service/sys_blk_mng/init_enter.rc) ;\
          echo \${service_name} >>./x86_dbf_status_before.log 2>&1 ;\
          line_no=\$((line_no + 1))
          class=\$(sed -n \"\${line_no}p\" /opt/arene/share/service/sys_blk_mng/init_enter.rc) ;\
          echo \${class} >>./x86_dbf_status_before.log 2>&1 ;\
          line_no=\$((line_no + 1))
          dbf_file=\$(sed -n \"\${line_no}p\" /opt/arene/share/service/sys_blk_mng/init_enter.rc | awk '{print \$2}') ;\
          /opt/arene/test/brysh dbread \${dbf_file} -d >>./x86_dbf_status_before.log 2>&1 ;\
          line_no=\$((line_no + 1))
          service_file=\$(sed -n \"\${line_no}p\" /opt/arene/share/service/sys_blk_mng/init_enter.rc) ;\
          echo "" >>./x86_dbf_status_before.log 2>&1 ;\
        done ;\
        exit 0"
        docker cp ${DOCKER_CONTAINER_NAME}:/home/default/x86_dbf_status_before.log ${test_path}/
        #################################################################################
        # start execute test
        #################################################################################
        echo "[INFO] Start execute the test for unit [${test_unit_name}]."
        echo "$ ${test_command}" >> ${test_log}
        if [[ ${test_unit_name} == "input" ]]; then
            timeout -k 10s 5m docker exec ${DOCKER_CONTAINER_NAME} bash -c "${test_command} >> ${docker_log_path}/${test_unit_name}_test_result.log 2>&1;exit"
            if [[ $?  -ne 0 ]]; then
                echo "unit ${test_unit_name} test timeout!"  >> ${test_log}
                log "[INFO ] unit ${test_unit_name} test timeout!" | tee -a ${test_result_file}
            fi
        else
            docker exec ${DOCKER_CONTAINER_NAME} bash -c "${test_command} 2>&1 | tee ;exit" >> ${test_log} 2>&1
        fi
        if [[ $?  -ne 0 ]]; then
            echo "Failed to execute the test for unit ${test_unit_name}."  >> ${test_log}
            echo "[ERR ] Failed to execute the test for unit ${test_unit_name}."
            # exit 1
        fi
        echo "Successfully execute the test for unit ${test_unit_name}."   >> ${test_log}
        echo "[INFO] Successfully execute the test for unit ${test_unit_name}."

        #################################################################################
        # copy test result from runtime docker
        #################################################################################
        echo "[INFO] Start copy the test result for unit ${test_unit_name}."
        docker exec ${DOCKER_CONTAINER_NAME} bash -c "dlt-logstorage-ctrl -s /var/log/dlt;\
                                                gzip -dk /var/log/dlt/journal.*.dlt.gz; \
                                                find /var/log/dlt/ -name "journal.*.dlt" | xargs dlt-convert -a >> /var/log/dlt/journal_dlt_convert.log; \
                                                cp -rf /tmp/${test_unit_name}_* ${docker_log_path}/ ;\
                                                cp -rf /var/log/dlt/ ${docker_log_path}/ ;\
                                                chown -R ${USER_ID}:${USER_ID} ${docker_log_path}/ ;\
                                                cd ${docker_log_path}/ ;\
                                                ps -ef > system_process.txt ;\
                                                journalctl > service_start.log ;\
                                                chown -R ${USER_ID}:${USER_ID} system_process.txt ;\
                                                chown -R ${USER_ID}:${USER_ID} service_start.log ;\
                                                exit 0"
        if [[ $? -ne 0 ]]; then
            echo "[ERR ] Failed to copy the test result for unit ${test_unit_name}."
            # exit 1
        fi
        echo "[INFO] Successfully copy the test result for unit ${test_unit_name}."
        docker exec ${DOCKER_CONTAINER_NAME} bash -c "ps -eo euser,pid,ppid,cmd | grep -v ps" > ${test_path}/test_after_process.log
        docker exec -u default ${DOCKER_CONTAINER_NAME} bash -c "\
        cd /home/default ;\
        :>./x86_dbf_status_after.log ;\
        for line_no in \$(grep -n -E '^service|service base_svc' '/opt/arene/share/service/sys_blk_mng/init_enter.rc' | cut -d ':' -f1); do \
          service_name=\$(sed -n \"\${line_no}p\" /opt/arene/share/service/sys_blk_mng/init_enter.rc) ;\
          echo \${service_name} >>./x86_dbf_status_after.log 2>&1 ;\
          line_no=\$((line_no + 1))
          class=\$(sed -n \"\${line_no}p\" /opt/arene/share/service/sys_blk_mng/init_enter.rc) ;\
          echo \${class} >>./x86_dbf_status_after.log 2>&1 ;\
          line_no=\$((line_no + 1))
          dbf_file=\$(sed -n \"\${line_no}p\" /opt/arene/share/service/sys_blk_mng/init_enter.rc | awk '{print \$2}') ;\
          /opt/arene/test/brysh dbread \${dbf_file} -d >>./x86_dbf_status_after.log 2>&1 ;\
          line_no=\$((line_no + 1))
          service_file=\$(sed -n \"\${line_no}p\" /opt/arene/share/service/sys_blk_mng/init_enter.rc) ;\
          echo "" >>./x86_dbf_status_after.log 2>&1 ;\
        done ;\
        exit 0"
        docker cp ${DOCKER_CONTAINER_NAME}:/home/default/x86_dbf_status_after.log ${test_path}/
        #################################################################################
        echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< [${test_unit_name}] test end time = [$(date +%Y/%m/%d-%H:%M:%S)][$(date +%s)] <<<<<<<<<<<<"
        echo ""
        echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> [${test_unit_name}] test result processing start >>>>>>>>>>>>"
        local unit_service_list=(${TEST_TARGET_SERVICE[${test_unit_name}]})

        echo "[INFO] unit [${test_unit_name}] Check test." >> ${test_result_file}

        echo "[INFO] grep "AllTests" *.xml check test results ." | tee -a ${test_result_file}
        grep "AllTests" ${test_path}/*.xml | tee -a ${test_result_file}
        echo "-------------------------------------------" | tee -a ${test_result_file}
        echo "" | tee -a ${test_result_file}

        echo "[INFO] grep [  FAILED  ] ${test_log} check errors case." | tee -a ${test_result_file}
        grep -a '\[  FAILED  \]' ${test_log} | tee -a ${test_result_file}
        echo "-------------------------------------------" | tee -a ${test_result_file}
        echo "" | tee -a ${test_result_file}

        echo "[INFO] Client Sanitizer check, grep -E AddressSanitizer|LeakSanitizer ${test_log} ." | tee -a ${test_result_file}
        grep -E 'AddressSanitizer|LeakSanitizer' ${test_log} | tee -a ${test_result_file}
        echo "-------------------------------------------" | tee -a ${test_result_file}
        echo "" | tee -a ${test_result_file}

        echo "[INFO] Server Sanitizer check, grep -E AddressSanitizer/LeakSanitizer ${test_path}/dlt/journal_dlt_convert.log ." | tee -a ${test_result_file}
        local sanitizer_flag="OK"
        server_sanitizer=$(grep -E 'AddressSanitizer|LeakSanitizer' ${test_path}/dlt/journal_dlt_convert.log | cut -d' ' -f16)
        if [[ ${server_sanitizer} ]]; then
          for ser in ${server_sanitizer[@]} ; do
            if [[ -n ${TEST_UNIT_SERVICE_NAME[${test_unit_name}]} ]];then
              services=(${TEST_UNIT_SERVICE_NAME[${test_unit_name}]//:/ })
              for test_ser in ${services[@]} ; do
                if [[ ${ser} =~ ${test_ser} ]]; then
                  sanitizer_flag="NG"
                  break
                fi
              done
              if [[ "NG" == ${sanitizer_flag} ]] ; then
                grep -E 'AddressSanitizer|LeakSanitizer' ${test_path}/dlt/journal_dlt_convert.log | tee -a ${test_result_file}
                break
              fi
            fi
          done
        fi
        echo "-------------------------------------------" | tee -a ${test_result_file}
        echo "" | tee -a ${test_result_file}
        
        # echo "[INFO] unit [${test_unit_name}] Check before test." >> ${test_result_file}
        # git diff --color -U1 ${test_path}/test_before_process.log ${test_path}/test_after_process.log | tee -a ${test_result_file}
        # echo "-------------------------------------------" >> ${test_result_file}
        # echo "" >> ${test_result_file}
        echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< [${test_unit_name}] test result processing  end  <<<<<<<<<<<<"
    done
    echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< [${1}_IT_${2}] docker_unit_test end time = [$(date +%Y/%m/%d-%H:%M:%S)][$(date +%s)] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
    echo ""
}

########################################################
# main
########################################################
while :
do
    echo "#####################################################################"
    echo "#         Please select one of the options below to proceed         #"
    echo "# Options:                                                          #"
    echo "#         1   RELEASE DOCKER install                                #"
    echo "#         2   SDK DOCKER install (from srv005 server)               #"
    echo "#         3   SDK DOCKER install (from 172.30.10.8)                 #"
    echo "#         4   bev test                                              #"
    echo "#         5   docker restart                                        #"
    echo "#         0   exit                                                  #"
    echo "#####################################################################"

    read -p "Please enter your options:" options
    echo ""
    
    case ${options} in
            1)
                read -p "Please enter your docker version (eg:20250409_0.9.0.0-rc.2):" dnver
                log "[NORMAL] >>>>>>Install RELEASE DOCKER Start !<<<<<<"
                install_docker_env ${dnver}
                log "[NORMAL] >>>>>>Install RELEASE DOCKER End  !<<<<<<"
                ;;
            2)
                log "[NORMAL] >>>>>> SDK DOCKER install (from srv005 server) Start !<<<<<<"
                install_sdk_docker "bev@srv005"
                log "[NORMAL] >>>>>> SDK DOCKER install (from srv005 server) End  !<<<<<<"
                ;;
            3)
                log "[NORMAL] >>>>>> SDK DOCKER install (from 172.30.10.8)  Start !<<<<<<"
                install_sdk_docker "wovendev@PC1798"
                log "[NORMAL] >>>>>> SDK DOCKER install (from 172.30.10.8)  End  !<<<<<<"
                ;;
            4)
                log "[NORMAL] >>>>>> bev test Start !<<<<<<"
                common_check
                image_tag=$(grep DOCKER_IMAGE_RUNTIME_TAG ${DOCKER_CONFIGS_FILE} | cut -d = -f2)
                log_date=${image_tag}_${DATE}
                mkdir -p ${LOG_PATH}/${log_date}
                test_log="${LOG_PATH}/${log_date}/sdk_build_test.log"
                : > ${test_log}
                start_run | tee -a ${test_log}
                count=1
                IT_COUNT=1
                while (( ${count} <= ${IT_COUNT} ))
                do
                    bev_unit_test "create" ${count} ${log_date} | tee -a ${test_log}
                    bev_unit_test "restart" ${count} ${log_date} | tee -a ${test_log}
                    let "count++"
                done
                normal_exit | tee -a ${test_log}
                log "[NORMAL] >>>>>> bev test End  !<<<<<<"
                ;;
            5)
                log "[NORMAL] -- docker restart --"
                docker_restart
                ;;
            0)
                log "[NORMAL] --exit 0--"
                exit 0
                ;;
            *)
                log "[ERROR ] ------wrong option = [ ${options} ]!!!------"
                ;;
    esac
done
